###############################################################################
## AUTHOR: ALAN YAN
## DATE LAST UPDATED: 07/26/2020
## PURPOSE: APPENDIX: ANALYZE YOUGOV CONJOINT DATA - PARTY
###############################################################################
rm(list = ls())

#### PACKAGES ####
library(pacman)
p_load(tidyverse,
       estimatr,
       broom,
       haven,
       hrbrthemes,
       cjoint,
       formula.tools, 
       DeclareDesign,
       emmeans,
       lmtest,
       cregg,
       gridExtra,
       egg,
       car,
       stargazer)

#### LOAD DATA ####
df <- read_rds("01-data/cleaned/yougov2-conjoint-cleaned.rds") %>%
  mutate(
    r_party = r_party - mean(r_party, na.rm = TRUE)
  )

#### ANALYZE DATA (NO CONTROLS) ####
#Do people prefer firms with these features?
work <- lm(work_binary ~ 
             r_party*as.factor(cje_corporate_gov) +
             r_party*as.factor(cje_corporate_resp) +
             r_party*as.factor(cje_firm_size) +
             r_party*as.factor(cje_political_donations) + 
             r_party*as.factor(cje_gender_ownership) + 
             r_party*as.factor(cje_healthcare) + 
             r_party*as.factor(cje_hours) + 
             r_party*as.factor(cje_training) + 
             r_party*as.factor(cje_location) + 
             r_party*as.factor(cje_sick_leave) + 
             r_party*as.factor(cje_parental_leave) + 
             r_party*as.factor(cje_race_ownership) + 
             r_party*as.factor(cje_retirement) + 
             r_party*as.factor(cje_income) +
             r_party*as.factor(cje_type_of_work) +
             r_party*as.factor(cje_union) +
             r_party*as.factor(cje_work_from_home) +
             r_party*as.factor(cje_team_work) +
             r_party*as.factor(cje_work_culture),
           data = df
) 
#Do people believe they'll have more power at some firms?
power <- lm(power_binary ~ 
              r_party*as.factor(cje_corporate_gov) +
              r_party*as.factor(cje_corporate_resp) +
              r_party*as.factor(cje_firm_size) +
              r_party*as.factor(cje_political_donations) + 
              r_party*as.factor(cje_gender_ownership) + 
              r_party*as.factor(cje_healthcare) + 
              r_party*as.factor(cje_hours) + 
              r_party*as.factor(cje_training) + 
              r_party*as.factor(cje_location) + 
              r_party*as.factor(cje_sick_leave) + 
              r_party*as.factor(cje_parental_leave) + 
              r_party*as.factor(cje_race_ownership) + 
              r_party*as.factor(cje_retirement) + 
              r_party*as.factor(cje_income) +
              r_party*as.factor(cje_type_of_work) +
              r_party*as.factor(cje_union) +
              r_party*as.factor(cje_work_from_home) +
              r_party*as.factor(cje_team_work) +
              r_party*as.factor(cje_work_culture),
            data = df
) 

#### tables S146 to S157 ####
# define dependent variables
dep_var_labels <- c("Prefer to work",
                    "More power")
covariate_labels <- names(work$coefficients)[-1] %>%
  stringr::str_replace("r_party", "Party (Republican)") %>%
  stringr::str_replace(":", " X ") %>%
  stringr::str_remove_all("as.factor\\(cje_corporate_gov\\)|as.factor\\(cje_corporate_resp\\)|as.factor\\(cje_firm_size\\)|as.factor\\(cje_political_donations\\)|as.factor\\(cje_gender_ownership\\)|as.factor\\(cje_healthcare\\)|as.factor\\(cje_hours\\)|as.factor\\(cje_training\\)|as.factor\\(cje_location\\)|as.factor\\(cje_sick_leave\\)|as.factor\\(cje_parental_leave\\)|as.factor\\(cje_race_ownership\\)|as.factor\\(cje_retirement\\)|as.factor\\(cje_income\\)|as.factor\\(cje_type_of_work\\)|as.factor\\(cje_union\\)|as.factor\\(cje_work_from_home\\)|as.factor\\(cje_team_work\\)|as.factor\\(cje_work_culture\\)")

stargazer(
  work,
  power,
  dep.var.labels = dep_var_labels[1:2],
  covariate.labels = covariate_labels,
  se = list(
    starprep(work, stat = "std.error", clusters = df$rid[!is.na(df$r_party)], se_type = "stata")[[1]],
    starprep(power, stat = "std.error", clusters = df$rid[!is.na(df$r_party)], se_type = "stata")[[1]]
  ),
  p = list(
    starprep(work, stat = "p.value", clusters = df$rid[!is.na(df$r_party)], se_type = "stata")[[1]],
    starprep(power, stat = "p.value", clusters = df$rid[!is.na(df$r_party)], se_type = "stata")[[1]]
  ),
  title = "Replicating Figure A16 work and power results",
  label = "tab:het-pid-interactions-work-power"
)

